package com.lvtongka.app.dialog

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.view.*
import android.widget.Toast
import com.lvtongka.app.R
import com.lvtongka.app.databinding.DialogFuncScanBinding
import com.lvtongka.app.http.common.WebConstants
import com.lvtongka.app.ui.webview.WebViewActivity

open class FuncScanDialog(context: Context, theme: Int) : Dialog(context, theme) {
    private var binding: DialogFuncScanBinding = DialogFuncScanBinding.inflate(layoutInflater)

    private var isXy = false
    init {
        window!!.decorView.setPadding(0, 0, 0, 0)
        window!!.setGravity(Gravity.RELATIVE_LAYOUT_DIRECTION or Gravity.BOTTOM)
        val lp = window!!.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = lp
        setContentView(binding.root)
        initView()
        initListener()
    }

    open fun hideDialog() {
        cancel()
        dismiss()
    }

    private fun initView() {

    }

    private fun checkRadio(){
        isXy = !isXy
        if (isXy) {
            binding.ivCb.setImageResource(R.mipmap.icon_pay_selected)
        } else {
            binding.ivCb.setImageResource(R.mipmap.icon_pay_select_un)
        }
    }

    private fun initListener() {
        setOnKeyListener { _, keyCode, event ->
            false
        }
        binding.tvCommit.setOnClickListener {
            if (isXy) {
                mListener?.onClickConfirm()
            } else {
                Toast.makeText(context, "请先同意付款用户服务协议", Toast.LENGTH_SHORT).show()
            }
        }
        binding.ivCb.setOnClickListener {
            checkRadio()
        }
        binding.tvXy.setOnClickListener {
            checkRadio()
        }
        binding.tvAgree.setOnClickListener {
            val intent = Intent(context, WebViewActivity::class.java)
            intent.putExtra(WebViewActivity.URL_KEY, WebConstants.PAY_PRIVACY)
            intent.putExtra(WebViewActivity.IS_WITHE, true)
            context.startActivity(intent)
        }
    }

    private var mListener: OnConfirmClickListener? = null

    interface OnConfirmClickListener {
        /**
         * 点击条目
         */
        fun onClickConfirm()
    }

    fun setOnItemClickListener(listener: OnConfirmClickListener) {
        this.mListener = listener
    }

}